
#include"../include/server.h"


void print_usage(char *program)
{
	printf("%s usages: %s\n",program,strerror(errno));
	printf("-p(port):serve listen port\n");
	printf("-h(help):help information\n");
    printf("-d(daemon):the server progame running in backgruand\n"); 
}

/*功能：命令行参数解析
 * argc；参数个数
 * argv：参数
 * 返回：成功返回端口，失败返回-1
 */
int argument_parse(int argc,char **argv)
{
	int               opt;
    int               port = -1;
    int               daemon_run=0;
    char              *program= NULL;

	struct option long_options[]=
	{
		{"port",1,NULL,'p'},
		{"h",0,NULL,'h'},
        {"daemon",0,NULL,'d'},
        {0,0,0,0}

	};

	while((opt = getopt_long(argc,argv,"p:h",long_options,NULL)) > 0)
	{
		switch(opt)
		{
			case 'p':
				port = atoi(optarg);
				break;	
			case 'h':
				print_usage(argv[0]);
				return -1;
            case 'd':
                 daemon_run = 1 ;
                  break ;
		
			default:
				print_usage(argv[0]);
				return -1;
				
		}      
	}

	if(port < 0)
	{
		print_usage(argv[0]);
		return -1;
	}

	return port;

    if(daemon_run == 1)         
    {
        printf("program %s running in backgrund\n", program);
        daemon(1,1) ;
    }
}
